package yogiro.controller;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import yogiro.dao.TourInfoDAO;
import yogiro.service.MemberService;
import yogiro.service.TourInfoService;
import yogiro.to.ListTO;
import yogiro.to.MemberTO;
import yogiro.to.TourInfoTO;
import yogiro.util.Utilities;

public class MemberController extends MultiActionController {
	private MemberService service;
	public MemberController(MemberService service){
		this.service = service;
	}
	public String showRegisterMember(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return "member/register_form";//viewName - 타일즈설정 <definition>의 name속성 값을 return
	}
	public void main(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = null;
		String tourWriterType = "0";
		ArrayList list = service.selectTourRecommend(tourWriterType);
		ArrayList list1 = new ArrayList();
		for(int i=0;i<list.size();i++){
			TourInfoTO tio = (TourInfoTO) list.get(i);
			list1.add(service.selectTourRecommend2(tio.getTourNo()));
		}
		ListTO listTo = new ListTO(list, list1);
		request.setAttribute("list", listTo);
	}
	
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception{
		main(request, response);
		return new ModelAndView("login");	
	}
	
	public ModelAndView loginSuccess(HttpServletRequest request, HttpServletResponse response) throws Exception{

		ModelAndView mv = null;
		MemberTO mto = null;
		try{
			String id = request.getParameter("id");
			String password = request.getParameter("password");
			mto = service.selectMemberById(id);
			if(id.equals(mto.getMemberId())){
				if(password.equals(mto.getMemberPassword())){
					HttpSession session= request.getSession();
					session.setAttribute("mto", mto);
					mv = new ModelAndView("jsonView","mto",mto);
				}else{
					mv = new ModelAndView("jsonView","mto",mto);
				}
			}
		}catch(Exception e){
			mv = new ModelAndView("jsonView","mto",mto);
		}
		return mv;
	}
	
	public ModelAndView logout (HttpServletRequest request, HttpServletResponse response, HttpSession session ) throws Exception{
		main(request, response);
		session.invalidate();
		return new ModelAndView("login");
	}
	public ModelAndView selectMemberList(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		ArrayList<MemberTO> list = service.selectMemberList();
		return new ModelAndView("member/member_list", "list",list);
	}
	
	public ModelAndView setInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session)throws SQLException{
		ModelAndView mv = null;
		int info =Integer.parseInt(request.getParameter("Info"));
		if(info==1){
			mv = new ModelAndView("member/set_info");
		}else if(info==0){
			MemberTO member = (MemberTO) session.getAttribute("mto");
			String id = member.getMemberId();
			String password = request.getParameter("password");
			String name = request.getParameter("name");
			String birthday = request.getParameter("birthday");
			int detail = Integer.parseInt(request.getParameter("detail"));
			int level = Integer.parseInt(request.getParameter("level"));
			String zipcode = request.getParameter("zipcode");
			String address1 = request.getParameter("address1");
			String address2 = request.getParameter("address2");
			member = new MemberTO(id,password,name,birthday, detail, level,zipcode,address1,address2);
			service.updateMemberInfo(member);
			member = service.selectMemberById(id); 
			session.setAttribute("mto", member);
			mv = new ModelAndView("member/show_info");
		}
		return mv;
	}

	public ModelAndView myInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws SQLException{

		MemberTO mto = (MemberTO)session.getAttribute("mto");
		return new ModelAndView("member/show_info","mto",mto);
	}

	public ModelAndView deleteMember(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws Exception{

		MemberTO mto = (MemberTO)session.getAttribute("mto");
		service.deleteMember(mto.getMemberId());
		session.invalidate();
		main(request, response);
		return new ModelAndView("login");
	}

	public ModelAndView registerMember(HttpServletRequest request, HttpServletResponse response) 
			throws SQLException{
		ModelAndView mv = null;
		String id = request.getParameter("id");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String birthday = request.getParameter("birthday");
		int detail = Integer.parseInt(request.getParameter("detail"));
		int level = 1;
		String zipcode = request.getParameter("zipcode");
		String address1 = request.getParameter("address1");
		String address2 = request.getParameter("address2");
		MemberTO mto = new MemberTO(id,password,name,birthday, detail, level, zipcode,address1,address2);
		System.out.println("MTO:"+mto);
		service.insertMember(mto);
		mv = new ModelAndView("jsonView");
		return mv;
	}
	
	public ModelAndView findZipcode(HttpServletRequest request, HttpServletResponse response)throws SQLException{
		String dong = request.getParameter("dong");
		System.out.println("동 : "+dong);
		ArrayList list = service.selectZipcodeByDong(dong);
		return new ModelAndView("findZipcode","list",list);
	}
	public ModelAndView serchId(HttpServletRequest request, HttpServletResponse response)throws SQLException{
		String id = request.getParameter("id");
		MemberTO mto = service.selectMemberById(id);
		System.out.println(id);
		System.out.println(mto);
		return new ModelAndView("jsonView","mto",mto);
	}
	public ModelAndView findMemberInfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("member/find_info");
	}
	public ModelAndView selectMemberIdByMemberInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String birthday = request.getParameter("birthday");
		int detail = Integer.parseInt(request.getParameter("detail"));
		System.out.println("dd : " +birthday+ "detail: "+detail);
		String id = service.selectMemberIdByMemberInfo(birthday, detail);
		System.out.println("id :"+id);
		return new ModelAndView("jsonView","id",id);
	}

}











